package com.example.anew.journey.mynewjourney.logic.resetPassword.perstenter;

import android.text.TextUtils;

import com.example.anew.journey.mynewjourney.base.MyApplication;
import com.example.anew.journey.mynewjourney.config.RBConstants;
import com.example.anew.journey.mynewjourney.logic.resetPassword.bean.ResetPasswordBean;
import com.example.anew.journey.mynewjourney.logic.resetPassword.view.ISResetPasswordView;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import cn.jesse.nativelogger.NLogger;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 杭州惟君医疗科技有限公司14
 * Created by 20433 on 2018/3/24
 *
 * @desc 东风山河绿，春已不折柳，繁花看不尽，处处惹人留
 * @desc 佛祖镇楼，bug辟容易
 */

public class ResetPasswordPersenter {
    private ISResetPasswordView mISResetPasswordView ;

    public ResetPasswordPersenter(ISResetPasswordView miSResetPasswordView) {
        this.mISResetPasswordView = miSResetPasswordView;
    }
    // 点击登陆按钮
   public void onClickLogin(){
      String inputVersionCode  =  mISResetPasswordView.getInputVersionCode();
      String phoneNumber  =  mISResetPasswordView.getPhoneNumber();
      String newPassword  =  mISResetPasswordView.getNewPassword();

       if(TextUtils.isEmpty(inputVersionCode))mISResetPasswordView.showToastInformation("请输入验证码");
       if(TextUtils.isEmpty(newPassword)) mISResetPasswordView.showToastInformation("请输入新密码");
       if(TextUtils.isEmpty(phoneNumber)) mISResetPasswordView.showToastInformation("请输入手机号");

       submit(inputVersionCode,phoneNumber,newPassword);

    }

    private void submit(String inputVersionCode, final String phoneNumber, final String newPassword) {
        String url = RBConstants.SERVER_URL+RBConstants.RESET_PASSWORD;
        OkHttpUtils
                .post()
                .url(url)
                .addParams("code", inputVersionCode)
                .addParams("tel",phoneNumber)
                .addParams("password", newPassword)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String NetworkResponseDate = response.body().string();
                        Gson gson =   new Gson();
                        NLogger.e("TAG","=======qwe====22==========="+NetworkResponseDate);
                        final ResetPasswordBean resetPasswordBean =    gson.fromJson(NetworkResponseDate,ResetPasswordBean.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(resetPasswordBean.errorCode ==200) {
                                    mISResetPasswordView.showToastInformation("密码重置成功,为您跳转到登录页面");
                                    MyApplication.getmHandler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mISResetPasswordView.toFirstChangeMainActivity();
                                            mISResetPasswordView.SendMessage(phoneNumber,newPassword);
                                        }
                                    },800);
                                }else{
                                    mISResetPasswordView.showToastInformation("密码重置失败");
                                }
                            }
                        });
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                     //   mISResetPasswordView.showToastInformation("请检查网络");
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });

    }

    // 设置手机号
    public void setPhoneNumber(String phoneNumber){
        mISResetPasswordView.setPhoneNumber(phoneNumber);
    }



}
